package org.jrpc.spring;

import org.jrpc.base.ServiceRegistry;
import org.apache.commons.collections4.MapUtils;
import org.jrpc.server2.RpcServer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * 使用spring 时候
 * Created by Administrator on 2017/8/30.
 */
public class InitializingBase extends RpcServer implements InitializingBean,ApplicationContextAware {

    private static final Logger logger = LoggerFactory.getLogger(InitializingBase.class);

    public InitializingBase(String serviceAddress) {
        super(serviceAddress);
    }

    public InitializingBase(String serviceAddress, ServiceRegistry serviceRegistry) {
        super(serviceAddress, serviceRegistry);
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        logger.info("启动");
        logger.debug("启动");
        this.start();
    }

    @Override
    public void setApplicationContext(ApplicationContext ctx) throws BeansException {
// 扫描带有 RpcService 注解的类并初始化 handlerMap 对象
        logger.debug("加载东西");
        Map<String, Object> serviceBeanMap = ctx.getBeansWithAnnotation(Service.class);
        if (MapUtils.isNotEmpty(serviceBeanMap)) {
            for (Object serviceBean : serviceBeanMap.values()) {
              /*  Service rpcService = serviceBean.getClass().getAnnotation(Service.class);
                System.out.println(serviceBean.getClass().getSuperclass());

                Class<?> interfaces[] = serviceBean.getClass().getInterfaces();//获得Dog所实现的所有接口
                String serviceName = rpcService.value();
                for (Class<?> inte : interfaces) {//打印
//                    System.out.println("Dog实现接口："+inte);
                    serviceName=inte.getName();
                }*/


//                System.out.println(serviceBean.getClass().getName());
//                logger.info(serviceBean.getClass().getName());

//                String serviceVersion = rpcService.version();
//                if (StringUtil.isNotEmpty(serviceVersion)) {
//                    serviceName += "-" + serviceVersion;
//                }

                /*handlerMap.put(serviceName, serviceBean);*/
            }
        }
    }


}
